﻿
using System.Collections;
using System.Collections.Generic;

using System;
using System.Text;
using System.Linq;
using OfficeOpenXml;

namespace Excel2CS
{
    internal class ExcelReader
    //public class ExcelReader
    {
        const int excelNameRow = 1;
        const int excelTypeRow = 2;
        const int excelCSNRow = 3; // C/S or N
        const int excelCommentRow = 4;
        const int excelCheckRow = 5;
        const int excelFirstValueRow = 6;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="excelFile">c:/table/hero.xlsx</param>
        /// <param name="csPath">c:/cs</param>
        public static void Read(string excelFile, string csPath )
        {
            if(!File.Exists(excelFile))
            {
                Console.WriteLine("file not exist:"+ excelFile);
                return;
            }
            if (!Directory.Exists(csPath))
                Directory.CreateDirectory(csPath);            

            string[] propertyTypes = null;
            string[] propertyNames = null;
            string[] propertyComment = null;
            var allItemRowList = new List<List<KeyValuePair<string, string>>>();

            // If you use EPPlus for Noncommercial personal use.
            //This will also set the Author property to the name provided in the argument.
            ExcelPackage.License.SetNonCommercialPersonal("My Name");

            using (var package = new ExcelPackage(excelFile))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets[0];
                int rowCount = worksheet.Dimension.Rows;
                int colCount = worksheet.Dimension.Columns;

                for (int row = 1; row <= rowCount; row++)
                {
                    Console.Write(row + "\t");
                    List<string> lineData = new();
                    for (int col = 1; col <= colCount; col++)
                    {
                        if(worksheet.Cells[3, col].Text == "N") //跳过标记为N的列
                            continue;
                        string str = worksheet.Cells[row, col].Text;
                        Console.Write(str + "\t");
                        lineData.Add(str);
                    }
                    Console.WriteLine();

                    var rowIndex = row;
                    if (rowIndex == excelTypeRow)
                    {
                        //行数据表示类型
                        propertyTypes = lineData.ToArray();
                    }
                    else if (rowIndex == excelNameRow)
                    {
                        //行数据表示变量名
                        propertyNames = lineData.ToArray();
                    }
                    else if (rowIndex == excelCommentRow)
                    {
                        //行数据表示注释
                        propertyComment = lineData.ToArray();
                    }
                    else if (rowIndex >= excelFirstValueRow)
                    {
                        //行数据表示变量值
                        List<KeyValuePair<string, string>> itemList = new();
                        for (int j = 0; j < propertyNames.Length; j++)
                        {
                            if (j < lineData.Count)
                                itemList.Add(new KeyValuePair<string, string>(propertyNames[j], lineData[j]));
                            else
                                itemList.Add(new KeyValuePair<string, string>(propertyNames[j], null));
                        }
                        if (int.TryParse(lineData[0], out _))
                            allItemRowList.Add(itemList);
                    }
                }
            }

            if (propertyNames == null || propertyNames.Length == 0
                    || propertyTypes == null || propertyTypes.Length == 0
                    || propertyNames.Length != propertyTypes.Length)
            {
                Console.Error.WriteLine("table data error!");
                return;
            }

            //类名
            string fileName = Path.GetFileNameWithoutExtension(excelFile); //hero
            fileName = fileName.First().ToString().ToUpper() + fileName.Substring(1); //首字母大写
            string className = fileName + "Table";
            //根据数据生成C#脚本
            var generator = new ScriptGenerator(className, propertyNames, propertyTypes, propertyComment, allItemRowList);//propertyValueDic
            string codeStr = generator.CreateCode();

            StreamWriter sw = new(csPath + "/" + className + ".cs");
            sw.WriteLine(codeStr);
            sw.Close();

            Console.WriteLine("Auto Scripts Success : " + className);
        }
    }
}



